package gs.eisenhower.swallet.utils.common;

import static gs.eisenhower.swallet.utils.constants.WalletConstants.EMPTY;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Utils to store and retrieve application's shared preferences. As Shared preferences are used as local storage of
 * data, here are utils to read/write data used commonly used throughout application or information shared between
 * screens.
 * 
 * @author mbadarau
 */
public class SharedPreferencesSingleton {

    /**
     * Name of the shared preferences properties file. The real name has beside it an ".xml" ending.
     */
    private static final String APP_SHARED_PREFS_NAME = "fr.pentalog.apps.android.ecotravel.preferences";

    private static final int ZERO = 0;

    /**
     * Prevent instantiation.
     */
    private SharedPreferencesSingleton() {
    }

    /**
     * Context of application - main activity.
     */
    private static Context context;

    /**
     * The shared preferences of the application.
     */
    private static SharedPreferences sharedPreferences;

    /**
     * The editor of shared preferences. Use it in synchronized methods or in methods synchronize the property itself.
     */
    private static Editor sharedPreferencesEditor;

    public static void initContext(Context context) {
        SharedPreferencesSingleton.context = context;
        sharedPreferences = SharedPreferencesSingleton.context.getSharedPreferences(APP_SHARED_PREFS_NAME,
                Context.MODE_PRIVATE);
        sharedPreferencesEditor = sharedPreferences.edit();
    }

    /**
     * Read a value of a key as a string.
     * 
     * @param key
     * @return value of key as string
     */
    public static final String getPropertyAsString(String key) {
        return sharedPreferences.getString(key, EMPTY);
    }

    /**
     * Save/store a value of a key as a string.
     * 
     * @param key key
     * @param value value as string
     */
    public static synchronized final void savePropertyAsString(String key, String value) {
        sharedPreferencesEditor.putString(key, value);
        sharedPreferencesEditor.commit();
    }

    /**
     * Read a value of a key as a string.
     * 
     * @param key
     * @return value of key as string
     */
    public static final int getPropertyAsInt(String key) {
        return sharedPreferences.getInt(key, ZERO);
    }

    /**
     * Save/store a value of a key as a string.
     * 
     * @param key key
     * @param value value as string
     */
    public static synchronized final void savePropertyAsInt(String key, int value) {
        sharedPreferencesEditor.putInt(key, value);
        sharedPreferencesEditor.commit();
    }

    public static synchronized final void removeProperty(String key) {
        sharedPreferencesEditor.remove(key);
        sharedPreferencesEditor.commit();
    }
}
